import {transformI18n} from "@/plugins/i18n";
import {OrderTongji} from "@/api/statistics";
import {getSalesList} from "@/api/crm";
import * as XLSX from 'xlsx'

import {onMounted, reactive, ref, type Ref, toRaw} from "vue";

export function useAccount(tableRef: Ref) {
  const form = reactive({
    utm_source: "",
    tab_id: "0",
    search_field2: "default",
    audit_status: "3",time:[]
  });
  const curRow = ref();
  const dataList = ref([]);
  const statusList = ref([]);
  const isShow = ref(false);
  const loading = ref(true);
  const isLinkage = ref(false);
  const isExpandAll = ref(false);
  const isSelectAll = ref(false);
  const salesman_arr = ref([]);



  const columns: TableColumnList = [
    {label: "类别", prop: "type",showOverflowTooltip:true,cellRenderer: ({ row, index }) => (
        <span style={row.color?("font-weight: bold;color:"+row.color):''}>{row.name}</span>
      )},
    {label: "业绩目标", prop: "sales_target", width: 100,sortable:true,cellRenderer: ({ row, index }) => (
        <span style={row.color?("font-weight: bold;color:"+row.color):''}>{row.sales_target}</span>
      )},
    {label: "业绩占比", prop: "sales_target_bili", width: 100,sortable:true,cellRenderer: ({ row, index }) => (
        <span style={row.color?("font-weight: bold;color:"+row.color):''}>{(row.sales_target_bili||0).toFixed(2)}%</span>
      )},
    {label: "待处理自定义占比", prop: "diy_bili", width: 140,sortable:true,cellRenderer: ({ row, index }) => (
        <span style={row.color?("font-weight: bold;color:"+row.color):''}>{(row.diy_bili||0).toFixed(2)}%</span>
      )},
    {label: "弃单自定义占比", prop: "diy_bili_qidan", width: 140,sortable:true,cellRenderer: ({ row, index }) => (
        <span style={row.color?("font-weight: bold;color:"+row.color):''}>{(row.diy_bili_qidan||0).toFixed(2)}%</span>
      )},
    {label: "待处理待补", prop: "daibu", width: 120,sortable:true,cellRenderer: ({ row, index }) => (
        <span style={"font-weight: bold;color:"+((row?.yingde_finished||0)>row.finished?'green':'red')}>{(row?.yingde_finished||0)>0?(row.yingde_finished-row.finished):0}</span>
      )},
    {label: "弃单待补", prop: "daibu", width: 100,sortable:true,cellRenderer: ({ row, index }) => (
        <span style={"font-weight: bold;color:"+((row?.yingde_qidan||0)>row.qidan?'green':'red')}>{(row?.yingde_qidan||0)>0?(row.yingde_qidan-row.qidan):0}</span>
      )},
  ];


  function formatDate(date) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    return `${year}-${month}-${day}`;
  }
  const today = new Date();
  const last30Days = new Date(today);
  last30Days.setDate(today.getDate() - 29);
  // form.time = [formatDate(last30Days),formatDate(today)]
  form.time = [formatDate(today),formatDate(today)]

  const getSalemanList = async function (page=1,limit=1000) {
    const {data} = await getSalesList({page,limit}); // 这里是返回一维数组结构，前端自行处理成树结构，返回格式要求：唯一id加父节点parentId，parentId取父节点id
    salesman_arr.value = data?.data;
  }

  const ApiData = ref([]);
  async function onSearch() {
    loading.value = true;
    // form.page = pagination.currentPage
    // form.limit = pagination.pageSize
    const {data} = await OrderTongji(toRaw(form), 'get');
    ApiData.value = data.list;
    dataList.value = data.list[0];
    filterData()
    setTimeout(() => {
      loading.value = false;
    }, 500);
  }
  function filterData(){
    dataList.value = ApiData.value[form.tab_id]||[];
  }

  const resetForm = formEl => {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  };


  /** 数据权限 可自行开发 */
  const searchCondList =  ref([])


  const filterMethod = (query: string, node) => {
    return transformI18n(node.title)!.includes(query);
  };

  onMounted(async () => {
    onSearch();
    getSalemanList();
  });

  return {
    form, isShow, curRow, loading, columns, dataList, isLinkage, isExpandAll, isSelectAll,
    statusList,salesman_arr,
    onSearch, resetForm,filterData,
    searchCondList,
     filterMethod, transformI18n
  };
}
